package zhongbo.java.air.dictionary.service.impl;

import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhongbo.java.air.common.exception.BusinessException;
import zhongbo.java.air.common.exception.ExceptionCode;
import zhongbo.java.air.common.exception.NoStackLogException;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.dictionary.mapper.AlertZoneMapper;
import zhongbo.java.air.dictionary.model.entities.AlertZone;
import zhongbo.java.air.dictionary.service.IAlertZoneService;

import java.util.Date;
import java.util.Objects;

/**
 * @author WeiJin
 * @version 1.0
 * @date 2025/3/31 11:26
 */
@Service
public class AlertZoneServiceImpl implements IAlertZoneService {

    @Autowired
    private AlertZoneMapper alertZoneMapper;

    @Override
    public AlertZone saveAlertZone(AlertZone alertZone) {
        // 参数校验
        validAlertZone(alertZone);
        // 区域业务编码唯一性校验
        // 修改时校验区域业务代码唯一性
        AlertZone dataAlertZone = queryByZoneCode(alertZone.getZoneCode());
        // 新增查询出来就说明zoneCode已存在
        if (Objects.nonNull(dataAlertZone)) {
            throw new BusinessException(ExceptionCode.ZONE_CODE_EXISTED);
        }

        // 新增时给当前时间作为创建时间和修改时间
        alertZone.setCreateTime(new Date());
        alertZone.setUpdateTime(new Date());
        int inserted = alertZoneMapper.insert(alertZone);
        // 根据影响表中记录数做出相应的返回
        if (inserted > 0) {
            return alertZone;
        }
        throw new BusinessException(ExceptionCode.SAVE_FAILED);
    }

    @Override
    public String delAlertZone(String id) {
        int deleted = alertZoneMapper.deleteById(id);
        // 影响数据库记录条数做出相应的返回
        if (deleted > 0) {
            return id;
        }
        throw new BusinessException(ExceptionCode.DELETE_FAILED);
    }

    @Override
    public AlertZone modifyAlertZone(AlertZone alertZone) {
        // 参数校验
        validAlertZone(alertZone);
        // 单独校验id
        if (CharSequenceUtil.isBlank(alertZone.getId())) {
            throw new BusinessException(ExceptionCode.ID_NOT_NULL);
        }

        // 修改时先查询要修改的飞机id是否存在
        if (Objects.isNull(queryById(alertZone.getId()))) {
            throw new BusinessException(ExceptionCode.ALERT_ZONE_NOT_EXIST);
        }

        // 修改时校验区域业务代码唯一性
        AlertZone dataAlertZone = queryByZoneCode(alertZone.getZoneCode());
        // 数据库中 禁飞区域信息的 zoneCode 存在且 与当前要被修改的飞机信息id不一致 说明已经存在
        if (Objects.nonNull(dataAlertZone) && !dataAlertZone.getId().equals(alertZone.getId())) {
            throw new BusinessException(ExceptionCode.ZONE_CODE_EXISTED);
        }

        // 修改时设置当前时间为修改时间
        alertZone.setUpdateTime(new Date());
        int updated = alertZoneMapper.updateById(alertZone);

        // 修改成功，查询出修改后的信息，返回
        if (updated > 0) {
            return queryById(alertZone.getId());
        }
        // 修改失败 抛出异常
        throw new BusinessException(ExceptionCode.UPDATE_FAILED);
    }

    @Override
    public AlertZone queryById(String id) {
        AlertZone alertZone = alertZoneMapper.selectById(id);
        // 查询不到，抛出不存在异常
        if (Objects.isNull(alertZone)) {
            throw new BusinessException(ExceptionCode.CURRENT_ID_NOT_EXIST);
        }
        return alertZone;
    }

    @Override
    public PageVo<AlertZone> pageAlertZone(Integer pageNumber, Integer pageSize, String zoneType,
                                           String orderBy, Boolean orderType) {
        QueryWrapper<AlertZone> wrapper = new QueryWrapper<>();
        IPage<AlertZone> page = new Page<>(pageNumber, pageSize);
        // 区域类型不为空，添加查询条件
        wrapper.eq(CharSequenceUtil.isNotBlank(zoneType), "zone_type", zoneType);
        // 排序字段不为空，添加相应的排序
        if (CharSequenceUtil.isNotBlank(orderBy)) {
            // 升序
            if (Boolean.TRUE.equals(orderType)) {
                wrapper.orderByAsc(orderBy);
            } else {
                // 降序
                wrapper.orderByDesc(orderBy);
            }
        }
        // 查询
        IPage<AlertZone> alertZonePage = alertZoneMapper.selectPage(page, wrapper);
        return new PageVo<>(alertZonePage);
    }

    @Override
    public AlertZone queryByZoneCode(String code) {
        LambdaQueryWrapper<AlertZone> wrapper = new LambdaQueryWrapper<AlertZone>().eq(AlertZone::getZoneCode, code);
        return alertZoneMapper.selectOne(wrapper);
    }

    private void validAlertZone(AlertZone alertZone) {
        // 非空校验
        if (Objects.isNull(alertZone)) {
            throw new NoStackLogException("请求参数不能为空");
        }
        if (CharSequenceUtil.isBlank(alertZone.getZoneCode())) {
            throw new NoStackLogException("zoneCode 不能为空");
        }
        if (Objects.isNull(alertZone.getZoneType())) {
            throw new NoStackLogException("zoneType 不能为空");
        }
        if (CharSequenceUtil.isBlank(alertZone.getVertices())) {
            throw new NoStackLogException("vertices 不能为空");
        }
        if (Objects.isNull(alertZone.getEffectiveStart())) {
            throw new NoStackLogException("effectiveStart 不能为空");
        }
        // 长度校验
        if (alertZone.getZoneCode().length() > 20) {
            throw new NoStackLogException("参数zoneCode长度最长为20");
        }

    }
}
